//
//  TRHUD.swift
//  CommonWidgets
//
//  Created by sqkj on 2021/8/6.
//

import UIKit
import MBProgressHUD

open class TRHUD: MBProgressHUD {

    public class func showText(text: String,mode:MBProgressHUDMode) {
        let view = viewWithShow()
        
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.label.text = text
        hud.mode = mode
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: 2)
    }
    
   public class func viewWithShow() -> UIView {
        var window = UIApplication.shared.keyWindow
        if window?.windowLevel != UIWindow.Level.normal {
            let windowArray = UIApplication.shared.windows
            
            for tempWin in windowArray {
                if tempWin.windowLevel == UIWindow.Level.normal {
                    window = tempWin;
                    break
                }
            }
        }
        return window!
    }
    
    public class func showLoading(_ info: String = "",view:UIView,time:TimeInterval = 0){
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.label.text = info
        hud.mode = MBProgressHUDMode.indeterminate
        hud.removeFromSuperViewOnHide = true
        if time > 0 {
            DispatchQueue.main.asyncAfter(deadline: .now() + time) {
                self.dismiss(view: view)
            }
        }
    }
    
    public class func showLoading(_ info: String = "",time:TimeInterval = 0) {
        let view = viewWithShow()
        self.showLoading(view: view,time: time)
    }
    
    public class func showVioceAnimations(_ view:UIView? = nil,customView:UIView,time:TimeInterval = 0){
        var showView = view
        if showView == nil {
            showView = UIApplication.shared.keyWindow
        }
        let hud = MBProgressHUD.showAdded(to:showView!, animated: true)
        hud.backgroundView.style = MBProgressHUDBackgroundStyle.solidColor
        hud.mode = MBProgressHUDMode.customView
        hud.removeFromSuperViewOnHide = true
        hud.customView = customView
        hud.isUserInteractionEnabled = false
        hud.contentColor = .clear
        hud.bezelView.color = .clear
        hud.bezelView.style = MBProgressHUDBackgroundStyle.solidColor
        hud.offset.y = -200
        if time > 0 {
            DispatchQueue.main.asyncAfter(deadline: .now() + time) {
                self.dismiss(view: showView!)
             }
        }
    }
    
    public class func hiddenVioceAnimations() {
        let view = UIApplication.shared.keyWindow! as UIView
        MBProgressHUD.hide(for: view, animated: true)
    }
    
    public class func dismiss(view:UIView) {
        MBProgressHUD.hide(for: view, animated: true)
    }
    
    public class func dismiss() {
        let view = viewWithShow()
        self.dismiss(view:view)
    }
    
    public class func showSuccess(_ status: String) {
        showText(text: status,mode:MBProgressHUDMode.text)
    }
    
    public class func showError(_ status: String) {
        showText(text: status,mode:MBProgressHUDMode.text)
    }

}
